package com.xxx.modules.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.modules.mapper.BrandMapper;
import com.xxx.modules.entity.Brand;
import com.xxx.modules.service.BrandService;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.xxx.modules.utils.TimeUtil;
/**
 * 品牌
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-30
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;



    /**
     *  获取所有品牌接口实现类
     * @param pageNum
     * @param pageSize
     * @param brand
     * @return
     */
    @Override
    public Result selectBrandList(Brand brand, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (brand.getBrandName() !=null){
            wrapper.like("brand_name",brand.getBrandName());
        }
        List<Brand> data = brandMapper.selectList(wrapper);
        return ResultUtil.success(1,"成功",new PageInfo<>(data));
    }


    /**
     * 获取单个品牌接口实现类
     * @param id
     * @return
     */
    @Override
    public Result selectBrandInfo(Integer id) {
        Brand brand = brandMapper.selectById(id);
        return ResultUtil.success(1,"成功",brand);
    }

    /**
     * 保存品牌接口实现类
     * @param brand
     * @return
     */
    @Override
    public Result saveBrandInfo(Brand brand) {
        QueryWrapper<Brand> brandNameWrapper = new QueryWrapper<>();
        brandNameWrapper.eq("brand_name",brand.getBrandName());
        Brand brandNameInfo = brandMapper.selectOne(brandNameWrapper);
        if (brandNameInfo != null){
            return ResultUtil.error(-1,"品牌名重复");
        }
        brand.setCreateTime(TimeUtil.getCurrentTime());
        brand.setUpdateTime(TimeUtil.getCurrentTime());
        brandMapper.insert(brand);
        return ResultUtil.success(1,"成功",null);
}

    /**
     * 更新品牌接口实现类
     * @param brand
     * @return
     */
    @Override
    public Result updateBrandInfo(Brand brand) {
        QueryWrapper<Brand> brandNameWrapper = new QueryWrapper<>();
        brandNameWrapper.eq("brand_name",brand.getBrandName()).ne("id",brand.getId());
        Brand brandNameInfo = brandMapper.selectOne(brandNameWrapper);
        if (brandNameInfo != null){
            return ResultUtil.error(-1,"品牌名重复");
        }
        brand.setUpdateTime(TimeUtil.getCurrentTime());
        brandMapper.updateById(brand);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除品牌删除接口实现类
     * @param id
     * @return
     */
    @Override
    public Result delBrandInfo(Integer id) {
        brandMapper.deleteById(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除品牌接口实现类
     * @param idList
     * @return
     */
    @Override
    public Result delBatchBrandInfo(String idList) {
        List<String> list = Arrays.asList(idList.split(","));
        brandMapper.deleteBatchIds(list);
        return ResultUtil.success(1,"成功",null);
    }



}